package com.sky.controller.admin;

import com.sky.Service.DishService;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Api(tags = "菜品相关接口")
@RestController(value = "adminDishController")
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取菜品分页数据
     *
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("获取菜品分页数据")
    @GetMapping("/page")
    public Result<PageResult> getByPage(DishPageQueryDTO dishPageQueryDTO) {
        System.out.println("dishPageQueryDTO:" + dishPageQueryDTO);
        PageResult byPage = dishService.getByPage(dishPageQueryDTO);
        return Result.success(byPage);
    }

    /**
     * 添加菜品信息
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("添加菜品信息")
    @PostMapping
    public Result<String> addDishInfo(@RequestBody DishDTO dishDTO) {
        System.out.println("dishDTO:" + dishDTO.toString());
        //将菜品status设置为1，表示启用
        dishDTO.setStatus(StatusConstant.ENABLE);
        dishService.addDishInfoWithFlavor(dishDTO);
        //删除缓存
        String key = "dish_" + dishDTO.getCategoryId();
        clearCache(key);
        return Result.success("菜品信息添加成功");
    }


    /**
     * 更新菜品状态
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("更新菜品状态")
    @PostMapping("/status/{status}")
    public Result<String> updateDishStatus(@PathVariable Integer status, Long id) {
        System.out.println("status:" + status);
        System.out.println("id:" + id);
        dishService.updateDishStatus(status, id);
        //根据id查询相应的菜品信息
        Dish dish = dishService.getById(id);
        //获取菜品分类id
        Long categoryId = dish.getCategoryId();
        String key = "dish_" + categoryId;
        //删除缓存
        clearCache(key);
        return Result.success("菜品状态更新成功");
    }

    /**
     * 根据id查询菜品信息
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品信息")
    @GetMapping("/{id}")
    public Result<DishDTO> getDishInfoById(@PathVariable Long id) {
        System.out.println("id:" + id);
        DishDTO dishById = dishService.getDishById(id);
        return Result.success(dishById);
    }

    /**
     * 修改菜品信息
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品信息")
    @PutMapping
    public Result<String> modifyDishInfo(@RequestBody DishDTO dishDTO) {
        System.out.println("dishDTO:" + dishDTO.toString());
        dishService.modifyDishInfo(dishDTO);
        //删除缓存
        clearCache("dish_*");
        return Result.success("菜品信息修改成功");
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result<String> deleteDishByIds(Long[] ids) {
        dishService.deleteDishByIds(ids);
        //删除缓存
        clearCache("dish_*");
        return Result.success("菜品信息删除成功");
    }

    /**
     * 根据分类id查询菜品列表
     *
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品列表")
    @GetMapping("/list")
    public Result<List<Dish>> selectDishesByCategoryId(Long categoryId) {
        System.out.println("categoryId:" + categoryId);
        List<Dish> dishes = dishService.selectDishesByCategoryId(categoryId);
        return Result.success(dishes);
    }

    /**
     * 清除缓存
     */
    private void clearCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}
